#region (c) 2014 THN Solutions LLC. - All rights reserved
/*
THN TELEPORT version 2

Copyright (c) 2014, THN Solutions LLC. ( www.ThnSolutions.com )
Author: Nguyễn, M. Hải                 ( www.Minh-Hai.com     )
All rights reserved.

This library is dual-licensed.
  + For commercial software, please obtain a commercial license from THN Solutions LLC.

  + For free software, this library is licensed under GPL version 3. A summary of GPLv3 is
    listed below. You should also find a copy of GPLv3 as file License_GPLv3.txt included
	with the source-code files.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License version 3 as published by
    the Free Software Foundation  of the License.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
#endregion
using System;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using Thn.Communication.Teleporting;

using Demo.Business;

[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class TeleportService : System.Web.Services.WebService
{
    public TeleportService () {

        //Uncomment the following line if using designed components 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string HelloWorld() {
        return "Hello World";
    }

    [WebMethod]
    public string TeleportRequest(string gateway, string method, string encodedArgs)
    {
        string result = string.Empty;

        //at this state, we already received the encoded message from webservice.
        //All we need to do now is to relay the very same encoded message to Teleport
        //To do so, we may use 2 different approaches

        #region Approach 1
        //register new gateway and let the teleport handle the rest.
        //this method might not be useful in web environment since the AppDomain for this web application
        //might not have expired. Thus, the Teleport object is still alive -> the business object
        //created from the last request is still alive.

        /*
        //register gateway
        Teleport.AddGateway("ProductService", new ProductService());

        //delegate request to Teleport
        result = Teleport.Teleporter.ProcessRequest(gateway, method, encodedArgs);
         */
        #endregion

        #region Approach 2
        //intentionally initialize a new business object instance. Then specifically
        //instruct the Teleport to process the request using the new instance

        if (gateway == "ProductService")
        {
            ProductService newInstance = new ProductService();
            result = Teleport.Teleporter.ProcessRequest(newInstance, method, encodedArgs);
        }
        else result = "Could not find gateway [" + gateway + "]";
        #endregion

        return result;
    }
}
